// 2025/10/10: 首个版本
// 2025/11/12: 优化tab control显示
// 2025/11/24: 优化暗色模式下的颜色显示

using System;
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using Avalonia.Media;
using Avalonia.Controls;
using ASEva;
using ASEva.UIAvalonia;
using ASEva.Utility;

namespace CommonSource
{
    partial class CheckLogs : DialogPanel
    {
        public CheckLogs()
        {
            InitializeComponent();
            SetResizableMode(400, 200, 690, 420);
            language = new LanguageSwitch(Resources, AgencyLocal.GetAppLanguage());
            Title = language["title"];
            DataContext = model;
            
            if (FuncManager.Run("System.GetDesignerModeFlag") == null)
            {
                addToItems(model.LocalTableItems, AgencyLocal.GetLogMessages());
                if (AgencyLocal.ClientSide) initializeRemote();
            }
        }

        public String IconResourceName
        {
            set => Icon = ResourceLoader.Load(value)?.ToAvaloniaWindowIcon();
        }

        private async void initializeRemote()
        {
            var msgs = await AgencyAsync.GetLogMessages();
            addToItems(model.RemoteTableItems, msgs ?? []);
        }

        private void addToItems(ObservableCollection<TableItem> target, LogMessage[] messages)
        {
            if (messages.Length == 0)
            {
                target.Add(new TableItem { Message = language["no-logs"] });
            }
            else
            {
                for (int i = messages.Length - 1; i >= 0; i--)
                {
                    var elem = messages[i];

                    IBrush textBrush = Brushes.DarkGray;
                    switch (elem.Type)
                    {
                        case LogLevel.Error:
                            textBrush = Brushes.Red;
                            break;
                        case LogLevel.Warning:
                            textBrush = Brushes.DarkOrange;
                            break;
                        case LogLevel.Info:
                            textBrush = App.DarkTheme ? Brushes.LimeGreen : Brushes.Green;
                            break;
                    }

                    var time = elem.Time.ToString();
                    var message = elem.Text + (elem.RepeatedCount > 1 ? (" (x" + elem.RepeatedCount + ")") : "");
                    
                    target.Add(new TableItem
                    {
                        Time = time,
                        Message = message,
                        Foreground = textBrush,
                    });
                }
            }
        }

        private partial class TableItem : ObservableObject
        {
            [ObservableProperty]
            private String time;

            [ObservableProperty]
            private String message;

            [ObservableProperty]
            private IBrush foreground = App.DarkTheme ? Brushes.White : Brushes.Black;
        }

        private partial class Model : ObservableObject
        {
            public bool IsClient => AgencyLocal.ClientSide;

            [ObservableProperty]
            private ObservableCollection<TableItem> localTableItems = new();

            [ObservableProperty]
            private ObservableCollection<TableItem> remoteTableItems = new();
        }

        private Model model = new();
        private LanguageSwitch language;
    }
}